package com.iweb.newa202104.filter;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/**
 * @Author: LGZ
 * @Date: 2021/5/9 22:12
 * @Description:
 */
@Slf4j
@WebFilter(urlPatterns = "/user/*", filterName = "userFilter")
/**
 * TODO 一些不用登陆也可以访问的接口例如注册登陆、查看学习资料，这些URL需要添加到EXCLUDE_URL
 * @Description 用户过滤器，过滤掉非法访问接口，即无权限访问
 * @Author lgz
 * @Date 2021/5/11 19:28
 */
public class UserFilter implements Filter {

    /**
     * TODO
     * @Description
     * @Author lgz
     * @Date 2021/5/11 19:27
     * EXCLUDE_URL : 不使用过滤器的URL
     */
    private final String[] EXCLUDE_URL = {"/login", "/register"};
    private final int PRE_URL_LENGTH = "/user".length();

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    /**
     * TODO user控制层写完再写过滤规则
     * @Description 过滤器
     * @Author lgz
     * @Date 2021/5/9 22:15
     * @param servletRequest :
     * @param servletResponse :
     * @param filterChain :
     * @return : void
     */
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        FilterUtil.commonFilter(servletRequest, servletResponse, filterChain, PRE_URL_LENGTH, EXCLUDE_URL);
    }

    @Override
    public void destroy() {

    }
}
